import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Org } from 'src/entity/Org'
import { UserInfo } from 'src/entity/Userinfo'
import { Connection, IsNull, Repository } from 'typeorm'

@Injectable()
export class OrgService {
  constructor(
    @InjectRepository(Org)
    private orgRepository: Repository<Org>,
    private connection: Connection,
  ) { }
  // 获取部门列表
  async getOrgList() {
    const sqlDataList = await this.orgRepository.find({
      where: {
        isDelete: IsNull(),
      },
    })
    return sqlDataList.map((item) => {
      return {
        label: item.orgName,
        value: item.id.toString(),
      }
    })
  }

  // 获取部门成员列表
  async getOrgMemberList(id: number) {
    let sqlData
    await this.connection.transaction(async (manager) => {
      const sqlUserInfo = await manager.find(UserInfo, {
        where: {
          orgId: id,
          isDelete: IsNull(),
        },
      })
      sqlData = sqlUserInfo.map((item) => {
        return {
          label: item.userName,
          value: item.userId.toString(),
        }
      })
    })
    return sqlData
  }
}
